/**
 * $Revision: 1.0 $
 * $Author: Eric Yang $
 * $Date: Aug 7, 2009 10:38:54 PM $
 *
 * Author: Eric Yang
 * Date  : Aug 7, 2009 10:38:54 PM
 *
 */
package com.gooex.service.company.impl;

import java.util.ArrayList;
import java.util.List;
import org.hibernate.Criteria;
import org.hibernate.Query;
import org.hibernate.criterion.Restrictions;
import org.c4j.string.StringTools;
import org.c4j.system.web.page.Page;

import com.gooex.domain.company.Company;
import com.gooex.domain.organization.User;
import com.gooex.service.BaseAppHibernateService;
import com.gooex.service.base.exception.CompanyException;
import com.gooex.service.company.ICompanyManager;

/**
 * @author Eric Yang
 * @version 1.0
 */
public class CompanyManagerImpl extends BaseAppHibernateService implements ICompanyManager {

	public Company getCompany(String id) {
		if (StringTools.isBlankOrNull(id))
			return null;
		return (Company) getHibernateTemplate().get(Company.class, id);
	}

	public List<Company> getUserCompanies(User user) {
		Query query = getSession().createQuery("from Company where user.id=:userId");
		query.setParameter("userId", user.getId());
		return query.list();
	}

	public long getUserCompanyNum(User user) {
		Query query = getSession().createQuery("select count(*) from Company where user.id=:userId");
		query.setParameter("userId", user.getId());
		return (Long) query.uniqueResult();
	}

	public List<Company> getCompanies() {
		return getHibernateTemplate().find("from Company");
	}

	public Company getCompanyByName(String name) {
		List<Company> companys = findCompaniesByName(name);
		if (companys != null && companys.size() == 1)
			return companys.get(0);
		return null;
	}

	public void saveCompany(Company company) {
		if (StringTools.isBlankOrNull(company.getId())) {
			company.setId(null);
			company.setUser(getCurrentUser());
		}
		getHibernateTemplate().saveOrUpdate(company);
	}

	public void removeCompany(Company company) throws CompanyException {
		company = getCompany(company.getId());
		getHibernateTemplate().delete(company);
	}

	public List<Company> findCompaniesByName(String name) {
		String hql = "from " + Company.class.getName() + " where name like ?";
		return getHibernateTemplate().find(hql, name);
	}

	public List<Company> getAccreditedCompanies(User user) {
		Criteria criteria = getSession().createCriteria(Company.class).setCacheable(true);
		criteria.add(Restrictions.eq("user", user));
		criteria.add(Restrictions.eq("accredited", true));
		return criteria.list();
	}

	public List<Company> getRegionCompanies(User user, String regionId) {
		Criteria criteria = getSession().createCriteria(Company.class).setCacheable(true);
		criteria.add(Restrictions.eq("user", user));
		criteria.add(Restrictions.eq("accredited", true));
		if (StringTools.isNotBlankOrNull(regionId))
			criteria.add(Restrictions.like("region.id", regionId + "%"));
		return criteria.list();
	}

	public Company getCompany(String userId, String companyId) {
		Criteria criteria = getSession().createCriteria(Company.class);
		criteria.add(Restrictions.eq("user.id", userId));
		criteria.add(Restrictions.eq("id", companyId));
		List<Company> result = criteria.list();
		if (result != null && result.size() > 0)
			return result.get(0);
		return null;
	}

	public List<Company> getCompanies(Page page, boolean accredited) {
		String hql = " from " + Company.class.getName() + " where accredited=?";
		List<Object> params = new ArrayList<Object>();
		params.add(accredited);

		return findHQLQuery(hql, params.toArray(), page);
	}

}
